package com.rbac.api.interceptor.component;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rbac.api.constant.AdminConstant;
import com.rbac.api.controller.enums.HintMessageEunm;
import com.rbac.api.exception.AdminAllException;
import com.rbac.api.mybatis.entity.AdminUsersEntity;
import com.rbac.api.mybatis.mapper.AdminUsersMapper;
import com.rbac.utils.commUtils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@Component
public class CheckLoginComponent {

    ///日志操作
    private static final LogUtil logger = LogUtil.getLogger(CheckLoginComponent.class);

    ///操作 redis
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    ///管理员mapper
    @Autowired
    private AdminUsersMapper adminUsersMapper;


    ///根据 token 检测是否登录，用户状态是否有效
    public void checkWhetherEntryByToken(HttpServletRequest request) throws  Exception {
        //header 里面获取 token
        String headerToken = request.getHeader(AdminConstant.ADMIN_HEADER_TOKEN_KEY);

        //判断 token 是否为空
        if(StringUtils.isEmpty(headerToken)){
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_PARAME_ERROR_EXCEPTION);
        }

        //重置token失效时间（有可能 token 在上一秒就失效了）
        boolean setExpireRes =  stringRedisTemplate.expire(headerToken, AdminConstant.ADMIN_SUPER_USERS_LIFE_CYCLE, TimeUnit.HOURS);
        if(!(setExpireRes)){
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_NO_LOGIN_ERROR_EXCEPTION);
        }

        //根据 token 获取管理员编号
        String userInfoJsonStr = stringRedisTemplate.opsForValue().get(headerToken);
        JSONObject userInfoArr = JSONArray.parseObject(userInfoJsonStr);
        if(userInfoJsonStr == null || userInfoArr == null || StringUtils.isEmpty(userInfoJsonStr) || StringUtils.isEmpty(userInfoArr) ){
            //提示 token获取失败
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_GET_TOKEN_ERROR_EXCEPTION);
        }
        Object userNoObj = userInfoArr.get("userno");//管理员编号
        Object userAccountObj = userInfoArr.get("account");//管理员登录账号
        if(userNoObj == null || userAccountObj == null){
            //提示获取数据失败
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_REDIS_GET_INFO_EXCEPTION);
        }

        String userNo = userNoObj.toString();
        AdminUsersEntity userInfoOne = adminUsersMapper.selectCheckLoginByUserNoMapper(userNo);
        if(userInfoOne==null){
            //提示获取数据失败
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_DB_GET_INFO_EXCEPTION);
        }



        //账号比对
        String userAccountStr = userAccountObj.toString();
        if(StringUtils.isEmpty(userInfoOne.getUserAccount()) || !(userInfoOne.getUserAccount().equals(userAccountStr))){
            //提示信息不一致
            throw new AdminAllException(HintMessageEunm.EXCEPTION_H_INFO_INCONSISTENT_EXCEPTION);
        }

        //用户状态
        if(StringUtils.isEmpty(userInfoOne.getUserStatus()) || !(userInfoOne.getUserStatus().equals(AdminConstant.ADMIN_USERS_EFFECTIVE_STATUS)) ){
            //提示被冻结
            throw new AdminAllException(HintMessageEunm.EXCEPTION_ACCOUNT_ALREADY_FROZEN_ERROR);
        }



    }




}
